﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.ComponentModel.DataAnnotations;

namespace final_darskade.Models
{
    public class DetailOfUser
    {
        private darskadeEntities tableEntities = new darskadeEntities();
        private darskadeViewsEntities viewEntites = new darskadeViewsEntities();

        private user _user;
        public user User
        {
            get { return _user; }
            set { _user = value; }
        }

        private role _role_of_user;
        public role Role_of_user
        {
            get { return _role_of_user; }
            set { _role_of_user = value; }
        }

        #region no implementation yet
        private List<KeyValuePair<privilege, klass>> _specialPrivileges;
        public List<KeyValuePair<privilege, klass>> SpecialPrivileges
        {
            get { return _specialPrivileges; }
            set { _specialPrivileges = value; }
        }
        #endregion

        public void fillProperties(int user_id)
        {
            _user = tableEntities.users.Single(u => u.user_id == user_id);
            _user.pass = "";
            _user.security_question = "";
            _user.security_answer = "";
            if (_user != null)
            {
                _role_of_user = (from r in tableEntities.roles
                                 where r.role_id == _user.role_id
                                 select r).Single();
            }
        }
    }

    public class RegexUtilities
    {
        public static bool IsValidEmail(string strIn)
        {
            // Return true if strIn is in valid e-mail format.
            return Regex.IsMatch(strIn,
                   @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
                   @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");
        }
    }

    public class editUser
    {

        public Int32 user_id { get; set; }
        [Required(ErrorMessage = "username required")]
        public String username { get; set; }
        [Required(ErrorMessage = "password required")]
        public String pass { get; set; }
        [Required(ErrorMessage = "role ID required")]
        public Int32 role_id { get; set; }
        [Required(ErrorMessage = "email required")]
        [RegularExpression(@"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
              @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$", ErrorMessage = "Email address is not valid")]
        public String email { get; set; }
        [Required(ErrorMessage = "first name required")]
        public String first_name { get; set; }
        [Required(ErrorMessage = "last name required")]
        public String last_name { get; set; }
        [Required(ErrorMessage = "entrance year required")]
        public DateTime entrance_year { get; set; }
    }
}